package edu.vt.tcga.clinical;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import edu.vt.tcga.HibernateUtil;

public class ClinicalPortionTable {

	private List<ClinicalPortion> values;

	// Accessor methods
	public List<ClinicalPortion> getValues() {
		return values;
	}
	public void setValues(List<ClinicalPortion> values) {
		this.values = values;
	}
	
	
	// Constructors
	public ClinicalPortionTable(){
		values = new ArrayList<ClinicalPortion>();
	}
	
	public ClinicalPortionTable(String filename){
		this();
		this.loadFromFile(filename);
	}
	
	
	// Utility functions
	public void loadFromFile(String filename){
		
		// Get current time
		long start = System.currentTimeMillis();
		
		System.out.println("Attempting to load clinical portion file: " + filename);
		
		try {
			BufferedReader in  = new BufferedReader(new FileReader(filename));
			String line = "";
			String delims = "\t";
	
			// Load Analyses
			// Skip the header line in the file.
			line = in.readLine();

			while( (line = in.readLine()) != null){
				String[] tokens = line.split(delims);


				String bcrSampleBarcode = tokens[0];
				String bcrPortionBarcode = tokens[1];
				int dayOfCreation = 0;
				int monthOfCreation = 0;	
				String weight = tokens[4];
				int yearOfCreation = 0;
				
				// Error checking
				try{
					dayOfCreation = Integer.parseInt(tokens[2]);
				}
				catch(NumberFormatException ne){
					System.out.println("Could not format dayOfCreation. Value read in as " + 
							tokens[2] +
							". Setting to 0.");
				}

				try{
					monthOfCreation = Integer.parseInt(tokens[3]);
				}
				catch(NumberFormatException ne){
					System.out.println("Could not format monthOfCreation. Value read in as " + 
							tokens[3] +
							". Setting to 0.");
				}

				try{
					yearOfCreation = Integer.parseInt(tokens[5]);
				}
				catch(NumberFormatException ne){
					System.out.println("Could not format yearOfCreation. Value read in as " + 
							tokens[5] +
							". Setting to 0.");
				}

				ClinicalPortion clinical = new ClinicalPortion(bcrSampleBarcode, 
						bcrPortionBarcode, 
						dayOfCreation, 
						monthOfCreation, 
						weight, 
						yearOfCreation);

				values.add(clinical);
			}
			
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
		// Get elapsed time in milliseconds
		long elapsedTimeMillis = System.currentTimeMillis()-start;
		double seconds = elapsedTimeMillis / 1000.0;
		
		System.out.println("Loaded clinical portion data in " + seconds + " seconds");
	}
	
	// Save the class out to the database
	public void saveToDatabase(){
		
		// Get current time
		long start = System.currentTimeMillis();
				
		try{
			Session session = HibernateUtil.getSession();
	        Transaction tx = session.beginTransaction();
	        int i = 0;
	        
	        for(ClinicalPortion clinical : this.getValues()){
	        	session.save(clinical);
		        
	            if ( i % 20 == 0 ) { //Same as the JDBC batch size
	                //flush a batch of inserts and release memory:
	                session.flush();
	                session.clear();
	            }
	            
		        i++;
	        }
	        
	        tx.commit();
	        session.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}

		// Get elapsed time in milliseconds
		long elapsedTimeMillis = System.currentTimeMillis()-start;
		double seconds = elapsedTimeMillis / 1000.0;
		
		System.out.println("Saved clinical portion data in " + seconds + " seconds");
	}
}
